package com.lsh.oms.core.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.model.order.AtpHoldDetail;
import com.lsh.oms.core.utils.model.Good;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author peter
 */
@Deprecated
public class GoodsFrozenServiceUtils extends BaseGoodUtils {

    private static Logger log = LoggerFactory.getLogger(GoodsFrozenServiceUtils.class);

    public static Map<Long, Good> getFrozenItem2GoodsMaps3(AtpHoldDetail atpHoldDetail, Map<Long, Long> orderDetailMap, String shopServiceUrl, Long venderId, String logSign) {

        List<Long> skuIds = new ArrayList<>();
        for (Long skuCode : atpHoldDetail.getSkuCodes()) {
            Long goodsCode = orderDetailMap.get(skuCode);
            if (null == goodsCode) {
                continue;
            }
            skuIds.add(goodsCode);
        }

        if (skuIds.isEmpty()) {
            return Collections.EMPTY_MAP;
        }
        List<BasicNameValuePair> paramPairs = new ArrayList<>(5);
        paramPairs.add(new BasicNameValuePair("sku_id", skuIds.toString()));
        paramPairs.add(new BasicNameValuePair("venderId", venderId + ""));
//        paramPairs.add(new BasicNameValuePair(GoodsConstants.SUPPLY_ORG_NAME, String.valueOf(atpHoldDetail.getSupplierOrg())));
//        paramPairs.add(new BasicNameValuePair(GoodsConstants.SUPPLY_DC_NAME, atpHoldDetail.getDc()));

        HttpEntity entity = new UrlEncodedFormEntity(paramPairs, Consts.UTF_8);
        shopServiceUrl = shopServiceUrl + SAAS_GETINFOS;
        log.info(logSign + "【请求商品服务url:{}】 【params is {}】", shopServiceUrl, JSON.toJSONString(paramPairs));
        JSONObject goodsInfoResp = doReqSaas(shopServiceUrl, entity, buildHeaders(venderId), logSign);

        if (!goodsInfoResp.getString("ret").equals(0)) {
            log.info(logSign + "【商品服务返回结果失败】");
            return Collections.emptyMap();
        }
        JSONArray contentArray = goodsInfoResp.getJSONArray("content");
        if (contentArray == null || contentArray.isEmpty()) {
            log.error("商品信息不存在！shopServiceUrl=" + shopServiceUrl);
            return Collections.emptyMap();
        }

        Map<Long, JSONObject> contentMap = new HashMap<>();
        for (int i = 0; i < contentArray.size(); i++) {
            JSONObject content = contentArray.getJSONObject(i);

            JSONObject skuInfo = content.getJSONObject("sku_info");
            if (skuInfo == null) {
                continue;
            }
            Long goodscode = skuInfo.getLong("sku_id");

            contentMap.put(goodscode, content);
        }
        Map<Long, Good> item2GoodsMap = new HashMap<>();

        // 2019-12-18
        for (Long skuCode : atpHoldDetail.getSkuCodes()) {
            Long goodsCode = orderDetailMap.get(skuCode);
            JSONObject content = contentMap.get(goodsCode);
            if (content == null) {
                log.error("商品信息不存在！content goodsCode=" + goodsCode);
                continue;
            }
            JSONObject goodsInfo = content.getJSONObject("goods_info");
            JSONObject itemInfo = content.getJSONObject("item_info");
            JSONObject skuInfo = content.getJSONObject("sku_info");

            if(goodsInfo == null){
                log.error("商品信息不存在 goodsInfo ！goodsCode=" + goodsCode);
                continue;
            }

            if(itemInfo == null){
                log.error("商品信息不存在 itemInfo ！goodsCode=" + goodsCode);
                continue;
            }
            if(skuInfo == null){
                log.error("商品信息不存在 skuInfo ！goodsCode=" + goodsCode);
                continue;
            }
            Good goods = new Good();
            goods.setGoodsCode(goodsCode);
            goods.setGoodsSaleUnit(skuInfo.getBigDecimal("sale_unit"));
            goods.setSkuCode(goodsInfo.getLong("item_id"));
            goods.setSupplySkuCode(goodsInfo.getString("goods_id"));
            goods.setSupplierOrg(atpHoldDetail.getSupplierOrg());
            item2GoodsMap.put(goodsCode, goods);
        }

        return item2GoodsMap;
    }


//    public static Map<String, Good> getFrozenCode2GoodsMaps(List<Long> params, String shopServiceUrl, ZoneSupplierDc zoneSupplierDc, String logSign,Long venderId) {
//
//        if (zoneSupplierDc == null) {
//            log.info(logSign + ",getFrozenCode2GoodsMaps zoneSupplierDc 为 null");
//            return null;
//        }
//
//        shopServiceUrl = shopServiceUrl + GETINFOS;
//        JSONArray skuIds = new JSONArray();
//        for (Long goodsCode : params) {
//            skuIds.add(Collections.singletonMap("sku_id", goodsCode));
//        }
//        List<BasicNameValuePair> paramPairs = new ArrayList<>(5);
//        paramPairs.add(new BasicNameValuePair("sku_ids", skuIds.toString()));
//        paramPairs.add(new BasicNameValuePair(GoodsConstants.SUPPLY_ORG_NAME, zoneSupplierDc.getSupplierOrg()));
//        paramPairs.add(new BasicNameValuePair(GoodsConstants.SUPPLY_DC_NAME, zoneSupplierDc.getDc()));
//
//        HttpEntity requestEntity = new UrlEncodedFormEntity(paramPairs, Consts.UTF_8);
//
//        log.info(logSign + "【getFrozenCode2GoodsMaps 请求商品服务url:{}】【param is {}】", shopServiceUrl, JSON.toJSONString(paramPairs));
//        Map<String, Object> goodsInfoResp = doReq(shopServiceUrl, requestEntity, buildHeaders(venderId), logSign);
//
//        if (!goodsInfoResp.get("ret").equals(0) || StringUtils.isEmpty(goodsInfoResp.get("content") + "")) {
//            log.info(logSign + "【getFrozenCode2GoodsMaps商品服务返回结果失败】");
//            return Collections.emptyMap();
//        }
//        JSONObject goodContent= JSON.parseObject(goodsInfoResp.get("res").toString()).getJSONObject("content");
//        Map<String, Good> code2GoodsMap = new HashMap<>();
//        Good goods;
//        for (Long goodsCode : params) {
//            JSONObject item = goodContent.getJSONObject(goodsCode.toString());
//            if (item == null) {
//                log.error(logSign + "【getFrozenCode2GoodsMaps 商品信息不存在！goodsCode:{}】", goodsCode);
//                continue;
//            }
//            goods = new Good();
//            goods.setGoodsCode(goodsCode);
//            goods.setGoodsSaleUnit(item.getBigDecimal("sale_unit"));
//            goods.setSkuCode(item.getLong("item_id"));
//            goods.setSupplySkuCode(item.getString("code"));
//
//            code2GoodsMap.put(item.getString("code"), goods);
//        }
//
//        return code2GoodsMap;
//    }

}
